<?php
class Promocoes extends Base{
        protected $_table = "promocoes";

        #
        private $nome = '';
        private $descricao = '';
        private $tweet = '';
        private $dataInicio = 0;
        private $dataFim = 0;
        private $ativo = 0;
        private $cupomDesconto = '';
        public function getNome() {
            return $this->nome;
        }

        public function setNome($nome) {
            $this->nome = $nome;
        }

        public function getDescricao() {
            return $this->descricao;
        }

        public function setDescricao($descricao) {
            $this->descricao = $descricao;
        }

        public function getTweet() {
            return $this->tweet;
        }

        public function setTweet($tweet) {
            $this->tweet = $tweet;
        }

        public function getDataInicio() {
            return $this->dataInicio;
        }

        public function setDataInicio($dataInicio) {
            $this->dataInicio = $this->formatDate($dataInicio);
        }

        public function getDataFim() {
            return $this->dataFim;
        }

        public function setDataFim($dataFim) {
            $this->dataFim = $this->formatDate($dataFim);
        }

        public function getAtivo() {
            return $this->ativo;
        }

        public function setAtivo($ativo) {
            $this->ativo = $ativo;
        }

        public function getCupomDesconto() {
            return $this->cupomDesconto;
        }

        public function setCupomDesconto($cupomDesconto) {
            $this->cupomDesconto = $cupomDesconto;
        }

        public function fetchForDate(){
            $sql = " SELECT id, nome, descricao, tweet, DATE_FORMAT(dataInicio,'%d/%m/%Y') as dataInicio, ";
            $sql.= " DATE_FORMAT(dataFim,'%d/%m/%Y') as dataFim, ativo, cupomDesconto FROM ". $this->_table;
            $sql.= " WHERE (dataInicio >= '" . date('Y-m-d') . "' OR dataFim <= '" . date('Y-m-d');
            $sql.= "') AND (ativo = '1')";

            
            
            $db = $this->db;
            $stm = $db->prepare($sql);
            $stm->execute();
            return $stm->fetchAll(PDO::FETCH_ASSOC);
        }


        /**
        * @filesource
        * @return void
        */

        public function fetchAll()
        {
            $sql = "SELECT id, nome, descricao, tweet, DATE_FORMAT(dataInicio,'%d/%m/%Y') as dataInicio, ";
            $sql.= "DATE_FORMAT(dataFim,'%d/%m/%Y') as dataFim, ativo, cupomDesconto FROM ". $this->_table;
            $db = $this->db;
            $stm = $db->prepare($sql);
            $stm->execute();
            return $stm->fetchAll(PDO::FETCH_ASSOC);
        }

        /**
        * @filesource
        * @return void
        */

        public function find()
        {
            $sql = "SELECT id, nome, descricao, tweet, DATE_FORMAT(dataInicio,'%d/%m/%Y') as dataInicio, ";
            $sql.= "DATE_FORMAT(dataFim,'%d/%m/%Y') as dataFim, ativo, cupomDesconto FROM ". $this->_table . " WHERE id=:id";
            $db = $this->db;
            $stm = $db->prepare($sql);
            $stm->bindValue(":id", $this->getId());
            $stm->execute();
            return $stm->fetch(PDO::FETCH_ASSOC);

        }


        
        protected function _insert(){
            $db = $this->getDb();
            $stm = $db->prepare('Insert into ' . $this->_table . '(nome,descricao,tweet,dataInicio,dataFim,ativo,cupomDesconto)
                Values(:nome,:descricao,:tweet,:dataInicio,:dataFim,:ativo,:cupomDesconto)');

            $stm->bindValue(':nome', $this->getNome());
            $stm->bindValue(':descricao', $this->getDescricao());
            $stm->bindValue(':tweet', $this->getTweet());
            $stm->bindValue(':dataInicio', $this->getDataInicio());
            $stm->bindValue(':dataFim', $this->getDataFim());
            $stm->bindValue(':ativo', $this->getAtivo());
            $stm->bindValue(':cupomDesconto', $this->getCupomDesconto());
            try
            {
                return $stm->execute();
            } catch (PDOExcetpion $e){
                echo  $e->getMessage();
            }


        }
        protected function _update(){
            $sql = 'update ' . $this->_table . '
                set nome=:nome, descricao=:descricao, tweet=:tweet, dataInicio=:dataInicio, dataFim=:dataFim, ativo=:ativo, cupomDesconto=:cupomDesconto where id=:id';
            $db = $this->getDb();
            $stm = $db->prepare($sql);

            $stm->bindValue(':nome', $this->getNome());
            $stm->bindValue(':descricao', $this->getDescricao());
            $stm->bindValue(':tweet', $this->getTweet());
            $stm->bindValue(':dataInicio', $this->getDataInicio());
            $stm->bindValue(':dataFim', $this->getDataFim());
            $stm->bindValue(':ativo', $this->getAtivo());
            $stm->bindValue(':cupomDesconto', $this->getCupomDesconto());
            $stm->bindValue(':id', $this->getId());

			$result = $stm->execute();
			
			
			
            return $result;
            
        }


        public function getTotalParticipantes($promocao){
            $sql = "SELECT count(id) AS total FROM usuarios WHERE promocaoId=:promocao";
            
            $db = $this->db;
            $stm = $db->prepare($sql);
            $stm->bindValue(":promocao", $promocao, PDO::PARAM_INT);
            $stm->execute();
            $result = $stm->fetch(PDO::FETCH_ASSOC);
            return $result['total'];
        }

     private function formatDate($data){
         $data = explode('/',$data);
         list($dia,$mes,$ano) = $data;
         return $ano . '-' . $mes . '-' . $dia;

     }
}

?>
